home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
utils
/
unix
/
unzip-5.12
/
inflate.c
< prev
next >
Wrap
Text File
|
1994-08-22
|
38KB
|
1,078 lines
/* inflate.c -- put in the public domain by Mark Adler
version c14o, 23 August 1994 */
/* You can do whatever you like with this source file, though I would
prefer that if you modify it and redistribute it that you include
comments to that effect with your name and the date. Thank you.
History:
vers date who what
---- --------- -------------- ------------------------------------
a ~~ Feb 92 M. Adler used full (large, one-step) lookup table
b1 21 Mar 92 M. Adler first version with partial lookup tables
b2 21 Mar 92 M. Adler fixed bug in fixed-code blocks
b3 22 Mar 92 M. Adler sped up match copies, cleaned up some
b4 25 Mar 92 M. Adler added prototypes; removed window[] (now
is the responsibility of unzip.h--also
changed name to slide[]), so needs diffs
for unzip.c and unzip.h (this allows
compiling in the small model on MSDOS);
fixed cast of q in huft_build();
b5 26 Mar 92 M. Adler got rid of unintended macro recursion.
b6 27 Mar 92 M. Adler got rid of nextbyte() routine. fixed
bug in inflate_fixed().
c1 30 Mar 92 M. Adler removed lbits, dbits environment variables.
changed BMAX to 16 for explode. Removed
OUTB usage, and replaced it with flush()--
this was a 20% speed improvement! Added
an explode.c (to replace unimplod.c) that
uses the huft routines here. Removed
register union.
c2 4 Apr 92 M. Adler fixed bug for file sizes a multiple of 32k.
c3 10 Apr 92 M. Adler reduced memory of code tables made by
huft_build significantly (factor of two to
three).
c4 15 Apr 92 M. Adler added NOMEMCPY do kill use of memcpy().
worked around a Turbo C optimization bug.
c5 21 Apr 92 M. Adler added the WSIZE #define to allow reducing
the 32K window size for specialized
applications.
c6 31 May 92 M. Adler added some typecasts to eliminate warnings
c7 27 Jun 92 G. Roelofs added some more typecasts (444: MSC bug).
c8 5 Oct 92 J-l. Gailly added ifdef'd code to deal with PKZIP bug.
c9 9 Oct 92 M. Adler removed a memory error message (~line 416).
c10 17 Oct 92 G. Roelofs changed ULONG/UWORD/byte to ulg/ush/uch,
removed old inflate, renamed inflate_entry
to inflate, added Mark's fix to a comment.
c10.5 14 Dec 92 M. Adler fix up error messages for incomplete trees.
c11 2 Jan 93 M. Adler fixed bug in detection of incomplete
tables, and removed assumption that EOB is
the longest code (bad assumption).
c12 3 Jan 93 M. Adler make tables for fixed blocks only once.
c13 5 Jan 93 M. Adler allow all zero length codes (pkzip 2.04c
outputs one zero length code for an empty
distance tree).
c14 12 Mar 93 M. Adler made inflate.c standalone with the
introduction of inflate.h.
c14b 16 Jul 93 G. Roelofs added (unsigned) typecast to w at 470.
c14c 19 Jul 93 J. Bush changed v[N_MAX], l[288], ll[28x+3x] arrays
to static for Amiga.
c14d 13 Aug 93 J-l. Gailly de-complicatified Mark's c[*p++]++ thing.
c14e 8 Oct 93 G. Roelofs changed memset() to memzero().
c14f 22 Oct 93 G. Roelofs renamed quietflg to qflag; made Trace()
conditional; added inflate_free().
c14g 28 Oct 93 G. Roelofs changed l/(lx+1) macro to pointer (Cray bug)
c14h 7 Dec 93 C. Ghisler huft_build() optimizations.
c14i 9 Jan 94 A. Verheijen set fixed_t{d,l} to NULL after freeing;
G. Roelofs check NEXTBYTE macro for EOF.
c14j 23 Jan 94 G. Roelofs removed Ghisler "optimizations"; ifdef'd
EOF check.
c14k 27 Feb 94 G. Roelofs added some typecasts to avoid warnings.
c14l 9 Apr 94 G. Roelofs fixed split comments on preprocessor lines
to avoid bug in Encore compiler.
c14m 7 Jul 94 P. Kienitz modified to allow assembler version of
inflate_codes() (define ASM_INFLATECODES)
c14n 22 Jul 94 G. Roelofs changed fprintf to FPRINTF for DLL versions
c14o 23 Aug 94 C. Spieler added a newline to a debug statement;
G. Roelofs added another typecast to avoid MSC warning
*/
/*
Inflate deflated (PKZIP's method 8 compressed) data. The compression
method searches for as much of the current string of bytes (up to a
length of 258) in the previous 32K bytes. If it doesn't find any
matches (of at least length 3), it codes the next byte. Otherwise, it
codes the length of the matched string and its distance backwards from
the current position. There is a single Huffman code that codes both
single bytes (called "literals") and match lengths. A second Huffman
code codes the distance information, which follows a length code. Each
length or distance code actually represents a base value and a number
of "extra" (sometimes zero) bits to get to add to the base value. At
the end of each deflated block is a special end-of-block (EOB) literal/
length code. The decoding process is basically: get a literal/length
code; if EOB then done; if a literal, emit the decoded byte; if a
length then get the distance and emit the referred-to bytes from the
sliding window of previously emitted data.
There are (currently) three kinds of inflate blocks: stored, fixed, and
dynamic. The compressor outputs a chunk of data at a time and decides
which method to use on a chunk-by-chunk basis. A chunk might typically
be 32K to 64K, uncompressed. If the chunk is uncompressible, then the
"stored" method is used. In this case, the bytes are simply stored as
is, eight bits per byte, with none of the above coding. The bytes are
preceded by a count, since there is no longer an EOB code.
If the data is compressible, then either the fixed or dynamic methods
are used. In the dynamic method, the compressed data is preceded by
an encoding of the literal/length and distance Huffman codes that are
to be used to decode this block. The representation is itself Huffman
coded, and so is preceded by a description of that code. These code
descriptions take up a little space, and so for small blocks, there is
a predefined set of codes, called the fixed codes. The fixed method is
used if the block ends up smaller that way (usually for quite small
chunks); otherwise the dynamic method is used. In the latter case, the
codes are customized to the probabilities in the current block and so
can code it much better than the pre-determined fixed codes can.
The Huffman codes themselves are decoded using a mutli-level table
lookup, in order to maximize the speed of decoding plus the speed of
building the decoding tables. See the comments below that precede the
lbits and dbits tuning parameters.
*/
/*
Notes beyond the 1.93a appnote.txt:
1. Distance pointers never point before the beginning of the output
stream.
2. Distance pointers can point back across blocks, up to 32k away.
3. There is an implied